function XVR(){	
	this._version = 0.1;
	this._error = xvr_error;
	this._error_code = xvr_error_code;
	
	this._listeners = {};
}

XVR.prototype = {
	
	constructor : XVR,

	/**************************************************************************
	 *						GETTERS & SETTERS 
	 *************************************************************************/
	version : function() {
		return this._version;
	},
	
	setError : function(code, message){
		this._error = message;
		this._error_code = code;
		
		//alert('Error');
		//this.fire('error');
	},
	
	getFormattedErrorMessage : function(){
		var res = 'Error ' + this._error_code + '\n';
		res += this._error;
		return res;
	},
	
	getLastErrorMessage : function (){
		return this._error;
	},
	
	getLastError : function (){
		return this._error_code;
	},
	/**************************************************************************
	 *						METHODS
	 *************************************************************************/
	
 	init : function (){
		log("init XVR");
		var container = document.getElementById(xvr_application_container_id);
		if(!container){
			this.setError(xvr_error_no_container_code, xvr_error_no_container);
			this.fire(xvr_events.onError);
			return;
		}
		
		var xvr_container = document.getElementById("xvr_container");
			
		var url = decodeURI(this.getURLParam('scriptName'));
		var engine = this.getURLParam ('engineVersion');
		var background = this.getURLParam('backgroundColor');
		var eParam =  this.getURLParam ('engineParam');
		
		var xvrObj = "<object id=\"" + xvr_obj_id + "\" width=\"100%\" height=\"100%\" type=\"application/x-oleobject\" \
						   classid=\"CLSID:5D589287-1496-4223-AE64-65FA078B5EAB\" \
						   codebase=\"http://client.vrmedia.it/XVRPlayer.cab#Version=1,0,0,1000\">\
						  <param name=\"ScriptName\" value=\"" + url + "\"></param>\
						  <param name=\"EngineVersion\" value=\"" + engine + "\"></param>\
						  <param name=\"BackgroundColor\" value=\"#" + background + "\"></param>\
						  <param name=\"EngineParam\" value=\"" + eParam + "\"></param>\
						  </object>";
			
		xvr_container.innerHTML = xvrObj;
     },
	 
	getURLParam : function(name){
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			switch(name){
				case "engineVersion": return "0153"; break;
				case "backgroundColor": return "FFFFFF"; break;
				case "scriptName" : return "test2.s3d.bin"; break;
				default: return ""; break;
			}
		else
			return results[1];
    },
    
	sendData: function (data){
		xvr_obj.DataIn(data);
	},
	
    /**************************************************************************
	 *						EVENTS HANDLING
	 *************************************************************************/
	
	isEventDefined : function(type) {
		return xvr_events[type] != undef;
	},
		
	addListener : function(event_type, listener) {
		if(!this.isEventDefined(event_type)) {
			this.setError(xvr_error_undefined_event_code, xvr_error_undefined_event);
			return;
		}
		if( typeof (this._listeners[event_type]) == undef)
			this._listeners[event_type] = [];
		this._listeners[event_type].push(listener);
	},
	
	fire : function(event_type){
        if(!this.isEventDefined(event_type)) {
			this.setError(xvr_error_undefined_event_code, xvr_error_undefined_event);
			this.fire(xvr_events.error);
			return;
		}
		var event = new Object();
		
        //if (!event.target){
            event.target = this;
            event.type = event_type;
        //}

        if (!event.type){  //falsy
            throw new Error("Event object missing 'type' property.");
        }

        if (this._listeners[event.type] instanceof Array){
            var listeners = this._listeners[event.type];
            for (var i=0, len=listeners.length; i < len; i++){
                listeners[i].call(this, 'on' + event);
            }
        }
    },

    removeListener: function(type, listener){
        if (this._listeners[type] instanceof Array){
            var listeners = this._listeners[type];
            for (var i=0, len=listeners.length; i < len; i++){
                if (listeners[i] === listener){
                    listeners.splice(i, 1);
                    break;
                }
            }
        }
    }

}